package com.lill.org.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.org.constant.PositionType;
import com.lill.org.model.PositionDictModel;
import com.lill.org.service.IEmployeeService;
import com.lill.org.service.IPositionDictService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.model.SysUserEntity;

/**
 * 职位字典管理Controller
 *
 * @author lill
 * @date 2019-04-24 15:25:16
 */
@RestController
@RequestMapping("org/positiondict")
public class PositionDictController {
    @Autowired
    private IPositionDictService orgPositionDictService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("orgpositiondict:list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	if(null!=params&&!params.containsKey("companyId")){
    		params.put("companyId", user.getCompanyId());
    	}
    	
        //查询列表数据
        Query query = new Query(params);

        List<PositionDictModel> orgPositionDictList = orgPositionDictService.queryList(query);
        int total = orgPositionDictService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(orgPositionDictList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("orgpositiondict:info")
    public R info(@PathVariable("id") String id) {
        PositionDictModel orgPositionDict = orgPositionDictService.queryObject(id);

        return R.ok().put("orgPositionDict", orgPositionDict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("orgpositiondict:save")
    public R save(@RequestBody PositionDictModel orgPositionDict,@CurrentUser SysUserEntity user) {
    	if(null==orgPositionDict.getCompanyId()||"".equals(orgPositionDict.getCompanyId())){
    		orgPositionDict.setCompanyId(user.getCompanyId());
    	}
        int result=orgPositionDictService.save(orgPositionDict);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("orgpositiondict:update")
    public R update(@RequestBody PositionDictModel orgPositionDict) {
        int result=orgPositionDictService.update(orgPositionDict);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("orgpositiondict:delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	PositionType[] positionList=PositionType.values();
    	List<String> idList =Arrays.asList(ids);
    	for (int i = 0; i < positionList.length; i++) {
    		if(idList.contains(positionList[i].getVal())){
    			return R.error(1, "系统职位不能删除！");
    		}
		}
    	
    	Map<String, Object> mapParams=new HashMap<String, Object>();
    	mapParams.put("companyId", user.getCompanyId());
    	mapParams.put("positionList", ids);
    	int num=employeeService.queryTotal(mapParams);
    	if(num>0){
    		return R.error(1, "职位已经被使用，不能删除！");
    	}
    	
        int result=orgPositionDictService.deleteBatch(ids);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {
        List<PositionDictModel> list = orgPositionDictService.queryList(params);
        return R.ok().put("list", list);
    }
    
    @RequestMapping("/dict")
    public R queryPositionSysDict() {
    	return R.ok().put("list", orgPositionDictService.queryPositionSysDict());
    }
}
